//
//  ViewController.m
//  YiChat20160706
//
//  Created by 李政 on 16/7/6.
//  Copyright © 2016年 ZhiYou. All rights reserved.
//

#import "ViewController.h"

#import "LoginViewController.h"

#import "HomeViewController.h"
#import "FileViewController.h"
#import "NewsViewController.h"
#import "PersonalViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self performSelector:@selector(animation) withObject:nil afterDelay:0.5];
    
}


- (void)animation
{
    
    NSLog(@"------%d",[self isNeedLogin]);
    
    if ([self isNeedLogin])
    {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        
        WINDOW.rootViewController = nav;

    }
    
    else
    {
        
        HomeViewController *homeVC = [[HomeViewController alloc] init];
        homeVC.tabBarItem.image = [UIImage imageNamed:@"main"];
        homeVC.tabBarItem.title = @"主页";
        UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:homeVC];
        
        
        NewsViewController *newsVC = [[NewsViewController alloc] init];
        newsVC.tabBarItem.image = [UIImage imageNamed:@"news"];
        newsVC.tabBarItem.title = @"新闻";
        UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:newsVC];
        
        FileViewController *fileVC = [[FileViewController alloc] init];
        fileVC.tabBarItem.image = [UIImage imageNamed:@"file"];
        fileVC.tabBarItem.title = @"文件";
        UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:fileVC];
        
        PersonalViewController *personalVC = [[PersonalViewController alloc] init];
        personalVC.tabBarItem.image = [UIImage imageNamed:@"person"];
        personalVC.tabBarItem.title = @"个人";
        UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:personalVC];
        
        
        UITabBarController *tabBar = [[UITabBarController alloc] init];
        
        tabBar.viewControllers = @[nav1,nav2,nav3,nav4];
        
        WINDOW.rootViewController = tabBar;
    }
    
    
    
    [UIView animateWithDuration:0.5 animations:^{
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:WINDOW cache:YES];
    }];
}

- (BOOL)isNeedLogin
{
    NSDate *currentDate = [NSDate date];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSDate *loseDate = [userDefaults objectForKey:LOSE_TIME];
    
    
    //第一次登陆或者过期的时候都是需要跳转到登陆界面的
    if (loseDate == nil || [currentDate compare:loseDate] == NSOrderedDescending)
    {
        return YES;
    }
    
    return NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
